package com.jiangruan.javainspect.service;

import com.alibaba.fastjson.JSONObject;
import com.jiangruan.javainspect.Constants;
import com.jiangruan.javainspect.bean.Getbestblockhash;
import com.jiangruan.javainspect.request.HttpUtils;

import java.util.Base64;
import java.util.HashMap;
import java.util.Map;

/**
 * @author:liyaqi
 * @email:3441520483@qq.com
 * @desc:
 * @datatime: 2021/12/13 20:17
 */
public class GetBestBlockHash{
    public Getbestblockhash getbestblockhash(){
        //准备用户信息
        String auth = Constants.BITCOIN_RPC_USER+":"+Constants.BITCOIN_RPC_PWD;
        //base64编码
        Base64.Encoder encoder = Base64.getEncoder();
       String authBase64 = encoder.encodeToString(auth.getBytes());
        //准备请求头
        Map<String,String> header = new HashMap<>();
        header.put("ContentType","application/json");
        header.put("Authorization","Basic " + authBase64);
        //准备请求数据
        Map<String,Object> body = new HashMap<>();
        body.put("id",1001);
        body.put("method", "getbestblockhash");
        body.put("params",null);
        body.put("json_rpc","2.0");
        byte[] bodyBytes = JSONObject.toJSONBytes(body);

       String result = HttpUtils.post(Constants.BITCOIN_PRC_URL,header,bodyBytes);
        Getbestblockhash bestblockhash = JSONObject.parseObject(result, Getbestblockhash.class);
        return bestblockhash;
    }
}
